$(function () {
    var csrfToken = $("meta[name='_csrf']").attr("content");
    var file=$("#file");
    file.fileinput({
        language: 'zh', //设置语言

        uploadUrl:'/teacher/upload', //上传的地址

        allowedFileExtensions: ['xls', 'xlsx'],//接收的文件后缀

        uploadAsync:true, //默认异步上传

        showUpload:true, //是否显示上传按钮

        initialCaption: "请选择excel文件",

        showRemove :false, //显示移除按钮

        showPreview :false, //是否显示预览

        enctype: 'multipart/form-data',

        showCaption:true,//是否显示标题

        autoReplace:true,//是否自动替换当前图片

        browseClass:"btn btn-primary ", //按钮样式

        dropZoneEnabled: false,//是否显示拖拽区域

        maxFileCount:2, //表示允许同时上传的最大文件个数

        validateInitialCount:true,

        ajaxSettings:{
            headers: {
                'X-CSRF-TOKEN':csrfToken
            }
        }

    }).on("fileuploaded", function (event, data, previewId, index) {
        var response=data.response;
        if(response.success){
            getTeachersByName(0);
            toastr.success(response.message);
        }else {
            toastr.error(response.message);
        }
        //重置
        file.fileinput('clear');//清空所有文件
        file.fileinput("reset");
        file.fileinput('refresh');
        file.fileinput('enable');
    });

    //解除绑定
    container.off("click");
    function getTeachersByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "teacherName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getTeachersByName(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getTeachersByName(0);
    });

    // 获取添加教师的界面
    $("#addTeacher").click(function() {
        $("#teacherDialogTitle").html("添加教师");
        axios({
            method:'get',
            url: "/teacher/add"
        }).then(function (response) {
            $("#teacherForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取编辑教师界面
    container.on("click","#edit-teacher",function () {
        $("#teacherDialogTitle").html("编辑教师");
        axios({
            method:'get',
            url:"/teacher/edit/"+$(this).attr('teacherId')
        }).then(function (response) {
            $("#teacherForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //提交操作，清空表单
    $("#teacherForm").submit(function() {
        var title=$("#teacherDialogTitle").html();
        var url,params,data;
        if ($.trim(title)=="教师授权"){
            var arr =[];
            $("input:checked").each(function(i){
                arr.push($(this).attr("roleVoId"));
            });
            url="/teacher/"+$('#teacherId').attr("teacherId")+"/authority";
            params={roleIds:arr.toString()};
        }else {
            url="/teacher";
            data=$('#teacherForm').serialize();
        }
        axios({
            method:"POST",
            url:url,
            data:data,
            params:params,
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#teacherForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getTeachersByName(0);
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });

        $(".teacherDialogClose").click();
        return false;
    });


    //删除教师
    container.on("click","#delete-teacher",function () {
        axios({
            method:'DELETE',
            url:"/teacher/"+$(this).attr('teacherId'),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            if (response.data.success) {
                // 从新刷新主界面
                getTeachersByName(0);
                toastr.success(response.data.message);
            } else {
                toastr.error(response.data.message);
            }
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //授权界面
    container.on("click","#edit-authority",function () {
        $("#teacherDialogTitle").html("教师授权");
        axios({
            method:'get',
            url:"/teacher/"+$(this).attr('teacherId')+"/authority"
        }).then(function (response) {
            $("#teacherForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //课程界面
    container.on("click","#teacher-timetables",function () {
        $("#teacherDialogTitle").html("教师课表");
        axios({
            method:'get',
            url:"/teacher/"+$(this).attr('teacherId')+"/timetables"
        }).then(function (response) {
            $('.modal-dialog').addClass("modal-lg");
            $("#teacherForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });


    //关闭Dialog界面
    $(".teacherDialogClose").click(function () {
        $('.modal-dialog').removeClass("modal-lg");
        $("#teacherForm").html("");
    });
});